<template>
  <el-dialog v-model="visible" :title="!dataForm.sysTenant.tenantId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="租户名称" prop="sysTenant.name">
        <el-input v-model="dataForm.sysTenant.name" placeholder="租户名称"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="sysTenant.contactPhone">
        <el-input v-model="dataForm.sysTenant.contactPhone" placeholder="联系方式"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="sysTenant.status">
        <el-radio-group v-model="dataForm.sysTenant.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否是超级租户" prop="sysTenant.isSuperTenant">
        <el-radio-group v-model="dataForm.sysTenant.isSuperTenant">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="分配门店"  prop="sysStoreList">
        <el-select multiple v-model="dataForm.sysStoreList" placeholder="分配门店">
          <el-option :label="data.storeName" v-for="data in storeList" :key="data.storeId" :value="data.storeId">{{ data.storeName }}</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const storeList = ref();

const dataForm = reactive({
  sysTenant: {
    tenantId: "",
    name: "",
    contactPhone: "",
    status: "",
    isSuperTenant: 0
  },
  sysStoreList: []
});

// const oldPasswordShow = computed(() => {
//   return changepasswordBol.value && dataForm.sysUser.id
// });
// const passwordShow = computed(() => {
//   return !dataForm.sysUser.id || (changepasswordBol.value && dataForm.sysUser.id);
// });

// const changePasswordHandle = () => {
//   changepasswordBol.value = !changepasswordBol.value;
// };

// const validatePassword = (rule: any, value: string, callback: (e?: Error) => any): any => {
//   if (!dataForm.sysUser.id && !/\S/.test(value)) {
//     return callback(new Error(t("validate.required")));
//   }
//   callback();
// };
// const validateConfirmPassword = (rule: any, value: string, callback: (e?: Error) => any): any => {
//   if (!dataForm.sysUser.id && !/\S/.test(value)) {
//     return callback(new Error(t("validate.required")));
//   }
//   if (dataForm.sysUser.password !== value) {
//     return callback(new Error(t("user.validate.confirmPassword")));
//   }
//   callback();
// };

const rules = ref({
  "sysTenant.name": [{ required: true, message: t("validate.required"), trigger: "blur" }],
  "sysTenant.status": [{ required: true, message: t("validate.required"), trigger: "blur" }],
  sysStoreList: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  // oldPassword: [{required: true, validator: validatePassword, trigger: "blur" }],
  // password: [{required: true, validator: validatePassword, trigger: "blur" }],
  // confirmPassword: [{ required: true, validator: validateConfirmPassword, trigger: "blur" }],
});

const init = (tenantId?: number) => {
  visible.value = true;
  dataForm.sysTenant.tenantId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  if (tenantId) {
    getInfo(tenantId);
  } else {
    getStoreList(null)
  }
};

// // 获取角色列表
// const getRoleList = () => {
//   return baseService.get("/sys/role/list").then((res) => {
//     roleList.value = res.data;
//   });
// };

// // 获取岗位列表
// const getPostList = () => {
//   return baseService.get("/sys/post/list").then((res) => {
//     postList.value = res.data;
//   });
// };

// 获取信息
const getInfo = (tenantId: number) => {
  baseService.get("/sys/systenant/" + tenantId).then((res) => {
    Object.assign(dataForm, res.data);
    getStoreList(tenantId)
  });
};

// 获取客户端信息
const getStoreList = (tenantId: any) => {
  baseService.get("/sys/sysstore/undistributedlist", {tenantId}).then((res) => {
    if (res.code === 200) {
      storeList.value = res.data;
    }
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }

    (!dataForm.sysTenant.tenantId ? baseService.post : baseService.put)("/sys/systenant", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    }).catch((err) => {
      ElMessage.error({
          message: err.message,
          duration: 2000
        });
    });
  });
};

defineExpose({
  init
});
</script>